package com.sxt.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.common.CodeMsg;
import com.sxt.common.Result;
import com.sxt.domain.BusCustomer;
import com.sxt.mapper.BusCustomerDao;
import com.sxt.query.BusCustomerQuery;
import com.sxt.service.BusCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Description: 客户业务接口实现类
 */
@Service
public class BusCustomerServiceImpl implements BusCustomerService {
    @Autowired
    private BusCustomerDao busCustomerDao;

    /**
     * 客户的分页查询
     *
     * @param query
     * @return
     */
    @Override
    public Result queryPage(BusCustomerQuery query) {
        Page<BusCustomer> busCustomerVOPage = PageHelper.startPage(query.getPage(), query.getLimit());
        busCustomerDao.selectList(query);
        return new Result(busCustomerVOPage.toPageInfo());
    }

    /**
     * 新增客户
     *
     * @param busCustomer
     * @return
     */
    @Override
    public Result add(BusCustomer busCustomer) {
        // 进行业务数据校验
        BusCustomerQuery query = new BusCustomerQuery();
        // 校验身份证是否重复
        query.setIdCard(busCustomer.getIdCard());
        BusCustomer customer = busCustomerDao.selectCustomerByKeywords(query);
        if (customer != null) {
            return new Result(CodeMsg.CUSTOMER_ID_CARD_EXIST_ERROR);
        }

        // 校验手机号是否重复
        query = new BusCustomerQuery();
        query.setPhone(busCustomer.getPhone());
        customer = busCustomerDao.selectCustomerByKeywords(query);
        if (customer != null) {
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }
        // 新增客户
        busCustomerDao.insert(busCustomer);
        return new Result();
    }

    /**
     * 修改客户信息
     *
     * @param busCustomer
     * @return
     */
    @Override
    public Result update(BusCustomer busCustomer) {
        // 进行业务数据校验
        BusCustomerQuery query = new BusCustomerQuery();
        // 校验身份证是否重复
        query.setIdCard(busCustomer.getIdCard());
        BusCustomer customer = busCustomerDao.selectCustomerByKeywords(query);
        //身份证号相同 存在两种情况:
        /*
         * 1. 历史数据 ID一致
         * 2. 其他数据 ID不一致
         * */
        if (customer != null && !customer.getId().equals(busCustomer.getId())) {
            return new Result(CodeMsg.CUSTOMER_ID_CARD_EXIST_ERROR);
        }

        // 校验手机号是否重复
        query = new BusCustomerQuery();
        query.setPhone(busCustomer.getPhone());
        customer = busCustomerDao.selectCustomerByKeywords(query);
        if (customer != null && !customer.getId().equals(busCustomer.getId())) {
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }
        // 更新客户数据
        busCustomerDao.update(busCustomer);
        return new Result();
    }

    /**
     * 导出客户信息
     *
     * @param query
     * @param response
     * @throws Exception
     */
    @Override
    public void exportCustomer(BusCustomerQuery query, HttpServletResponse response) throws Exception {
        // 与客户端相关的输出流
        ServletOutputStream os = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        // 客户信息.xls是弹出下载对话框的文件名，不能为中文，中文需要自行编码
        String fileName = URLEncoder.encode("客户信息.xls", "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        // 符合条件的所有的客户
        List<BusCustomer> busCustomers = busCustomerDao.selectList(query);
        //将客户转化为Excel数据流
        // 通过工具类创建writer，默认创建xls格式
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.addHeaderAlias("id", "编号");
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("phone", "手机号");
        writer.addHeaderAlias("address", "地址");
        writer.addHeaderAlias("idCard", "身份证号");
        writer.addHeaderAlias("sex", "性别");
        writer.addHeaderAlias("createTime", "创建时间");
        writer.addHeaderAlias("updateTime", "修改时间");
        // 将数据输出
        // 将Excel数据流输出给浏览器
        writer.write(busCustomers, true);
        writer.flush(os, true);
    }

    /**
     * 将Excel中的客户信息批量导入数据库
     *
     * @param file
     * @return
     * @throws Exception
     */
    @Override
    public Result batchAddCustomer(MultipartFile file) throws Exception {
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        // 将数据转化对象
        // 解析Excel文件
        List<BusCustomer> busCustomers = reader.readAll(BusCustomer.class);
        // 批量新增客户
        busCustomerDao.batchInsert(busCustomers);
        return new Result();
    }
}
